<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/7
 * Time: 1:51
 */

namespace app\api\controller\v3;

use app\api\controller\v2\Auth;
use app\common\model\ArticleCommentReply;
use app\common\model\MessageCenter;
use app\common\model\Recode;
use app\common\model\User;

use think\Db;

/**个人中心相关接口
 * @title
 * @desc
 * @url
 * @version 2.0.0
 * Class Demo
 * @package app\api\controller\v2
 */
class Personal extends Auth
{
    protected $restActionList = ['index', 'read', 'save', 'update', 'delete'];

    /**
     * @title
     * @method GET
     * @desc  获取我的信息
     * @return \think\response\Json
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        //判断用户是否绑定准了号
        $uen = (new User())->check_uen($this->user);

        $RecodeModel = new Recode();
        $MsgModel = new MessageCenter();
        $uid = $this->user['id'];

        //        将用户头像和档案头像统一
        $map = ['uid' => $this->user['id'], 'isself' => 1];
        $rea = $RecodeModel->getSelfRecode($map);

        if ($rea)
        {
            //若用户表无头像或和档案头像不统一
            if ($rea['avatar'])
            {
                if ((!$this->user['avatar'] || $this->user['avatar'] == "") || ((strcmp($rea['avatar'], $this->user['avatar']) !== 0)))
                {
                    try
                    {
                        Db::table('app_user')->where('id', $this->user['id'])->update(['avatar' => $rea['avatar']]);
                    }
                    catch (\Exception $e)
                    {
                        \think\Log::write('个人中心用户头像统一失败 ： ' . $e->getMessage(), 'error', true);
                    }
                }
            }

            //若档案没有头像
            if ($this->user['avatar'])
            {
                if (!$rea['avatar'] || $rea['avatar'] == "")
                {
                    try
                    {

                        Db::table('app_recode')->where($map)->update(['avatar' => $this->user['avatar']]);


                    }
                    catch (\Exception $e)
                    {
                        \think\Log::write('个人中心档案头像统一失败 ： ' . $e->getMessage(), 'error', true);
                    }
                }
            }

            $recode = $RecodeModel->getRecodeWithAstro(['id' => $rea['id']]);

            $sex = $this->user['sex'] ?: 1;

            $data = [
                'id' => $recode['id'],
                'uen' => $uen ?: $this->user['uen'],
                'name' => $recode['name'],
                'sex' => $sex,
                'avatar' => $recode['avatar'],
                'sun' => $recode['sun'],
                'moon' => $recode['moon'],
                'asc' => $recode['asc'],
            ];

            //获取用户全部未读系统消息id
            $system_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 1]);
            //获取用户全部未读服务消息id
            $server_msg_id = $MsgModel->getAllUserMessage(['uid' => $uid, 'type' => 2]);
            $msg_count = count($system_msg_id) + count($server_msg_id);

            // 获取回复的阅读情况
            $replyModel = new ArticleCommentReply();
            $unread_ids = $replyModel->getUnReadReply(['ucid' => $this->user['uc_id'], 'uid' => $this->user['id']]);
            if ($unread_ids)
            {
                $reply_count = count($unread_ids);
            } else
            {
                $reply_count = 0;
            }
            $array1['msg_count'] = $msg_count + $reply_count;
            $array = array_merge($data, $array1);
            return show(200, '成功', $array);
        }
        return show(0, '获取失败', '缺少档案');
    }
}
